<?php

namespace App\Http\Controllers\Manage;

use App\Model\Admin;
use App\Model\Role;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Input;

class RoleController extends Controller
{
    //定义一个数组  用来排列查询选项或列表头
    protected static $field = [
        ['id'=>1,'navigation'=>'编号'],
//        ['id'=>2,'navigation'=>'创建时间'],
        ['id'=>2,'navigation'=>'更新时间'],
        ['id'=>3,'navigation'=>'权重'],
        ['id'=>4,'navigation'=>'角色名称'],
        ['id'=>5,'navigation'=>'状态'],
    ];

    /**
     * 角色列表
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */

    public function index()
    {
        #获取用户角色id
        $role_id = session('user.role_id');
        if($role_id != 1){
            return back()->with('msg','权限不足');
        }
        #获取角色信息 权重越大、更新时间越近的优先显示
        $roleInfo = Role::getInfo();
        #获取表格头信息
        $navigation = self::$field;
        return view('manage.system.role.index',['roleInfo'=>$roleInfo,'navigation'=>$navigation,'active1'=>16,'active2'=>2]);
    }

    /**
     * 设置角色状态 启用/禁用
     */
    public function setStatus()
    {
        $id = Input::get('id');
        $status = Input::get('status');
        $res = Role::setStatus($id,$status);
        if($res){
            return [
                'error'=>0,
                'msg'=>'设置成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'设置失败！'
            ];
        }
    }

    /**
     * 删除角色
     * @return $this
     */
    public function delRole()
    {
        $id = Input::get('id');
        $res = Role::delRole($id);
        if($res){
            return [
                'error'=>0,
                'msg'=>'删除成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'删除失败！'
            ];
        }
    }

    public function getAllUser(){
        $role_id = Input::get("id");
        //获取所有员工
        $all_admin = Admin::where("status",1)->select("id","nickname")->get();
        //获取改组已经选择的员工
        $admin_role = DB::table("admin_role")->where("role_id",$role_id)->get();
        if($admin_role){
            foreach ($all_admin as &$value){
                foreach ($admin_role as $val){
                    if($value['id'] == $val->admin_id){
                        $value['is_check'] = 1;
                    }
                }
            }
        }
        die($all_admin);
    }
    //设置管理员组
    public function setAdminRole(){
        DB::enableQueryLog();
        $admin_ids = Input::get("admin_ids");
        if(!$admin_ids){
            die('ok');
        }
        $role_id = Input::get("id");
        $obj = DB::table("admin_role");
        $obj->where("role_id",$role_id)->delete();
        foreach ($admin_ids as $id){
            $obj->insertGetId(['admin_id'=>$id,"role_id"=>$role_id]);
        }
        die('ok');
    }

    /**
     * 跳转添加角色页
     * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
     */
    public function add()
    {
        return view('manage.system.role.add_role',['active1'=>16,'active2'=>2]);
    }

    /**
     * 添加角色
     * @return array
     */
    public function addRole()
    {
        #保存到数据库
        $res = Role::addRole(Input::all());
        if($res){
            return [
                'error'=>0,
                'msg'=>'添加成功！'
            ];
        }else{
            return [
                'error'=>1,
                'msg'=>'添加失败！'
            ];
        }
    }
}
